package com.service.impl;

import com.controller.PaginationSupport;
import com.entity.Department;
import com.form.Message;
import com.mapper.DepartmentMapper;
import com.mapper.EmployeeMapper;
import com.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    public int count() {
        return departmentMapper.countOfDepart();
    }

    @Override
    public PaginationSupport<Department> checkDepartments(int pageNo, int pageSize) {
        int totalCount = count();
        int startIndex = PaginationSupport.convertFromPageToStartIndex(pageNo, pageSize);
        List<Department> departmentList = departmentMapper.findPage(startIndex, pageSize);
        PaginationSupport<Department> departments = new PaginationSupport<Department>(departmentList, totalCount, pageSize, startIndex);
        return departments;
    }

    @Override
    public Message updateDepart(Department department) {
        Department fatherDepartment = findDepartmentById(department.getFatherDepartmentId());
        if(fatherDepartment==null){
            return new Message(0, "the fatherDepartmentId is invaild");
        }
        if (departmentMapper.updateDepart(department) == 1) {
            return new Message(1, "Update successful!");
        }
        return new Message(0, "Unknown error.");
    }

    @Override
    public Message addDepart(Department department) {
        Department fatherDepartment = findDepartmentById(department.getFatherDepartmentId());
        if (fatherDepartment == null ){
            return new Message(0, "the fatherDepartmentId is invaild");
        }
        boolean status = departmentMapper.addDepart(department);
        return new Message(1, "Add successful!");
    }

    @Override
    public Message deleteDepartmentById(int departmentId) {
        //判断这个部门有没有子部门，若有，则不成功
        int countOfChildDepart = departmentMapper.getDepartNumByFatherDepartId(departmentId);
        if (countOfChildDepart != 0) {
            return new Message(0, "This department have " + countOfChildDepart + " children department,you can`t delete it.");
        }
        //判断这个部门还有没有员工，有员工则无法删除
        int countOfEmployee = employeeMapper.getEmployeeNumByDepartId(departmentId);
        if (countOfEmployee != 0) {
            return new Message(0, "This department have " + countOfEmployee + " employees ,you can`t delete it.");
        }
        int status = departmentMapper.deleteDepartByDepartId(departmentId);
        if (status >= 1) {
            return new Message(1, "delete successful");
        }
        return new Message(0, "unknown exception");
    }

    public int countOfKeys(String keys) {
        return departmentMapper.getCountOfDepartByKeys(keys);
    }

    @Override
    public PaginationSupport<Department> findDepartsByKeys(int pageNo, int pageSize, String keys) {
        int totalCount = countOfKeys(keys);
        int startIndex = PaginationSupport.convertFromPageToStartIndex(pageNo, pageSize);
        List<Department> departments = departmentMapper.findPageByKeys(keys, startIndex, pageSize);
        PaginationSupport<Department> departmentList = new PaginationSupport<Department>(departments, totalCount, pageSize, totalCount);
        return departmentList;
    }

    private Department findDepartmentById(long departmentId) {
        return departmentMapper.findDepartmentById(departmentId);
    }
    @Override
    public int countDepartmentByRanking(String ranking) {

        return departmentMapper.countOfDepartmentByRanking(ranking);
    }
}
